# coding=utf-8
# ☯ Author: ChinaPython
# ☯ Email : chinapython@yeah.net
# ☯ Date  : 2019-09-11
import os
import time
import PySimpleGUI as sg

# All the stuff inside your window. This is the PSG magic code compactor...
layout = [
    [
        sg.Text(text='程序路径:'),
        sg.Input(
            do_not_clear=True,
            default_text='',
            text_color='red',
            key='file'
        ),
        sg.FileBrowse(button_text='载入', key='exe'),
    ],
    [
        sg.Text(text='存储路径:'),
        sg.Input(
            do_not_clear=True,
            default_text='',
            text_color='red',
            key='folder'
        ),
        sg.FolderBrowse(button_text='载入', key='path'),
    ],
    [
        sg.Text(text='监控周期(次)'),
        sg.Slider(range=(10, 30), default_value=20, orientation='h', key='times', size=(24.9, 10)),
        sg.Button(button_text='启动目标', key='start_a'),
        sg.Button(button_text='停止目标', key='kill'),
    ],
    [
        sg.Text(text='监控时长(秒)'),
        sg.Slider(range=(10, 80), default_value=30, orientation='h', key='timeout', size=(24.9, 10)),
        sg.Button(button_text='启动监控', key='start_b'),
        sg.Button(button_text='退出监控', key='logout'),

    ],
]


def auto_scan():
    while True:
        global queue
        queue = []
        queue.insert(0, sum(os.path.getsize(folder + '/' + temp) for temp in os.listdir(folder)))
        if len(queue) != int(times):
            time.sleep(int(timeout))
            continue
        queue = queue[:20]
        if len(set(queue)) != 1:
            continue
        # kill process
        os.system('taskkill /F /IM {}'.format(exe))
        time.sleep(5)
        # start exe
        os.system('start {}'.format(file))


# Create the Window
window = sg.Window('BMW download program monitor', layout, icon='icon.ico')
# Event Loop to process "events"

while True:
    event, values = window.Read()
    file, folder, times, timeout = values['file'], values['folder'], values['times'], values['timeout']
    exe = str(file).split('/')[-1]
    # logout the system
    if event == 'start_a':
        # start target
        if ':' not in file or '.exe' not in file:
            sg.Popup('程序参数缺失', icon='icon.ico', auto_close=True, title='')
            continue
        if ':' not in folder:
            sg.Popup('路径参数缺失', icon='icon.ico', auto_close=True, title='')
            continue
        # start exe
        os.system('start {}'.format(file))
    # kill target
    if event == 'kill':
        # start target
        if ':' not in file or '.exe' not in file:
            sg.Popup('程序参数缺失', icon='icon.ico', auto_close=True, title='')
            continue
        if ':' not in folder:
            sg.Popup('路径参数缺失', icon='icon.ico', auto_close=True, title='')
            continue
        os.system('taskkill /F /IM {}'.format(exe))
    # start monitor
    if event == 'start_b':
        if ':' not in file or '.exe' not in file:
            sg.Popup('程序参数缺失', icon='icon.ico', auto_close=True, title='')
            continue
        if ':' not in folder:
            sg.Popup('路径参数缺失', icon='icon.ico', auto_close=True, title='')
            continue
        auto_scan()
    # quit
    if event == 'logout' or event is None:
        break
window.Close()

if __name__ == '__main__':
    pass
